<!DOCTYPE html>
% # variables:
% # book

% import os.path
% import os
% import datetime as DT
% import xml.etree.ElementTree as ET

% import lonote.civa.putil

% booktitle = book.title()
% bookuuid = book.book_uuid()
% ebook = ET.fromstring(book.book_xml())
% note_count = len(ebook.findall('.//note')) + 1

<html lang="">
    <head>
        <meta charset="utf-8" />
        <title>Manage "{{book.title()}}"</title>
        <meta name="description" content="{{booktitle}} Configure" />

        <script src="/static/js/jquery-1.9.1.js"></script>
        <script src="/static/js/jquery-ui-1.10.1.custom.min.js"></script>

        <script src="/static/js/URI.js"></script>
        <script src="/static/js/shortcut.js"></script>
        <script src="/static/js/lonote.js"></script>

        <link rel="stylesheet" href="/static/css/jquery-ui/pepper-grinder/jquery-ui.css" />
        <link rel="stylesheet" href="/static/css/default.css" type="text/css" />
        <link rel="stylesheet" href="/static/css/config.css" type="text/css" />
        <!--
        <script>
            // 自動增加行高，可放到 keyup 中
            function auto_height(textarea){
                var adjustedHeight=textarea.clientHeight;
                adjustedHeight=Math.max(textarea.scrollHeight, adjustedHeight);
                if (adjustedHeight>textarea.clientHeight){
                    textarea.style.height=adjustedHeight+'px';
                }
                // 先縮短再伸長
                // textarea.style.height = 10 + 'px';
                //textarea.style.height = textarea.scrollHeight + 'px';
            };
        </script>
        <script>
            function init_textarea(){
                $('textarea').each(function(index, elem){ auto_height(elem); });
            }
            $(document).ready(init_textarea);
        </script>
        <style>
            /* textarea { overflow: hidden; } */
        </style>
        -->
    </head>

    <body>
        <article id="config_content">
            <!-- <p><a href="/last">↶ Content page</a></p> -->
            <!-- <p><a href="/config">↶ Configure page</a></p> -->
            <h1>Management of {{book.title()}}</h1>
            <p>You can manage your notebook in this page...</p>
            <table class="bookinfo">
                <!-- <tr><th>item</th><th>value</th></tr> -->
                <tr><td>Note Count</td><td>{{note_count}}</td></tr>
            <table>
            <!--
            <ul>
                <li><a href="#stylesheet">Stylesheet</a></li>
                <li><a href="#extrahead">Extra Head</a></li>
            </ul>
            -->
            <p><br/></p>

            <a name="stylesheet"></a><h2>Stylesheet</h2>
            <p class="description">Changing appearance by some CSS code.</p>
            <p class="description">Hint: All your note content are under the "#main_content" element.</p>
            <form method="POST" action="/extra/{{bookuuid}}/book.css?uploadmode=form">
                % usercss = book.extra('book.css')
                % usercss_default = '@charset "utf8";\n\n/* For example: Set article\'s background color as gray... */\n\n/*\n\n#main_content {\n    background-color: gray;\n}\n\n*/'
                <input value="CSS Update" type="submit" /><br/>
                <textarea name="data" rows="30" cols="60">{{usercss or usercss_default}}</textarea>
            </form>
            <p><br/></p>

            <a name="extrahead"></a><h2>Extra Head</h2>
            <p class="description">The following content will be insert to all note page's head block.</p>
            <p class="description">Hint: You can import some extra setting for every note page.</p>
            <form method="POST" action="/extra/{{bookuuid}}/extra_head?uploadmode=form">
                % extrahead = book.extra('extra_head')
                % extrahead_default = '<!-- Example 1: Import the MathJax math rendering library... -->\n\n<!--\n\n    <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML.js"></script>\n\n-->\n\n<!-- Example 2: Import a extra css theme... -->\n\n<!-- <link rel="stylesheet" href="/static/css/extra/light.css" /> -->\n<!-- <link rel="stylesheet" href="/static/css/extra/moonlight.css" /> -->\n<!-- <link rel="stylesheet" href="/static/css/extra/orange.css" /> -->'
                <input value="Extra Head Update" type="submit" /><br/>
                <textarea name="data" rows="15" cols="60">{{extrahead or extrahead_default}}</textarea>
            </form>
            <p><br/></p>

            <h2>Flush & Download</h2>
            % fpath = book.fpath
            % fname = os.path.basename(fpath)

            <p class="description">Download your whole notebook.</p>
            <form method="POST" action="/config/book/{{bookuuid}}/{{fname}}?cmd=flush">
                <input value="Flush & Download the Current Version" type="submit" /><br/>
            </form>

            <form method="POST" action="/config/book/{{bookuuid}}/{{fname}}?cmd=get">
                % stat = os.stat(fpath)
                % size_mb = "{:.3}".format(stat.st_size / 1024 / 1024)
                % mtime = DT.datetime.fromtimestamp(stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S %z')
                <input value="Download the Last Flush Version" type="submit" title="Modified time (guess): {{mtime}} Size: {{size_mb}} MB" /><br/>
            </form>

            <form method="POST" action="/config/book/{{bookuuid}}/{{fname}}?cmd=get_backup">
                % bak_fpath = lonote.civa.putil.chext(fpath, 'bak')
                % if os.path.isfile(bak_fpath):
                    % bak_stat = os.stat(bak_fpath)
                    % bak_size_mb = "{:.3}".format(bak_stat.st_size / 1024 / 1024)
                    % bak_mtime = DT.datetime.fromtimestamp(bak_stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S %z')
                    <input value="Download the Backup Version" type="submit" title="Modified time (guess): {{bak_mtime}} Size: {{bak_size_mb}} MB"/>
                % else:
                    <input value="Download the Backup Version" type="submit" disabled="disabled" title="No backup version available."/>
                % end

            </form>
            <p><br/></p>

            <h2>Search Index</h2>
            <p class="description">If you move a book between on two or more server manually, the search result may not fully correct.</p>
            <p class="description">For fix it, please re-index your notebook.</p>
            <form method="POST" action="?cmd=reindex">
                <input value="Re-index This Notebook" type="submit" title="Bigger book need more time to do it, please wait..." />
            </form>
            <p><br/></p>

            <h2>Delete Book</h2>
            <p class="description">Destroy a book is an immoral decision.</p>
            <form method="POST" action="?cmd=kill">
                <input class="icon" style="background-color: darkred; color: red; border: 1px solid #000000; border-radius: 3px;" value="Delete!" type="submit" title="This's not a good idea..." onclick="return confirm('Are you really want to delete {{booktitle}} ...?');" />
                <input name="fname" type="hidden" value="{{fname}}" />
            </form>
            <p><br/></p>

        </article>

        <script>
            $(window).on('load', lo.scr.restore);
            $(window).on('beforeunload', lo.scr.save);
            $(window).blur(lo.scr.save);
        </script>

        <!-- 讀入 sidebar -->
        <script>
            function sidebar_load(){
                function get_success(data){
                    $('body').append(data);
                };
                var sidebar_loc = '/sidebar';
                $.get(sidebar_loc, get_success);
            };
            $(window).one('load', sidebar_load);
        </script>

    </body>
</html>


